<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Account extends CI_Controller{
		
		
		function __construct()
		{
			parent::__construct();
			
			//load the default database
			$this->load->model("User");
			
		}
		
		//function to show all the web pages
		public function view($page = "update")
		{
			if( ! file_exists('application/views/account/' . $page . '.php') )
			{
				show_404();
			}
			
			$this->load->view('template/header', array(
				"current" => "",
				"css" => '<link href="/css/login.css" rel="stylesheet" type="text/css" />'
			));
			$this->load->view('account/' . $page);
			$this->load->view('template/footer');
		}
				
		//web serivce for user login
		public function login()
		{
			$this->load->library('session');
						
			$this->output->set_content_type('application/json');
	
			$UserId = $this->input->get("UserId", true);
			$Password = $this->input->get("Password", true);
			
			if($UserId && $Password)
			{
				$user = $this->User->checkPassword($UserId, $Password);
				
				if( $user !== FALSE )
				{
					$this->session->set_userdata(array("user" => $user));
					return $this->output->set_output(json_encode($user));
					
				}else{
					return $this->output->set_output(json_encode(array("error" => "Password or UserId error")));
				}
			}
			
			$this->output->set_output(json_encode(array("error" => "Unkown Error")));
		}
		
		//web servie to get user info
		public function userinfo(){
			
			$this->load->library('session');
			
			$this->output->set_content_type('application/json');
						
			$this->output->set_output(json_encode($this->session->userdata("user")));
		}
		
		//web servie to get user by id
		public function checkUserById($UserId){
			
			if($this->User->getUserById($UserId))
				return $this->output->set_output(1);
			return $this->output->set_output(0);
		}
		
		//web servie to get user by id
		public function checkUserByCellPhone($CellPhone){
			
			if($this->User->getUserByCellPhone($CellPhone))
				return $this->output->set_output(1);		
			return $this->output->set_output(0);
		}
		
		
		//web servie to get user by id
		public function checkUserByEMail($EMail){
			
			if($this->User->getUserByEMail($EMail))
				return $this->output->set_output(1);		
			return $this->output->set_output(0);
		}
		
		//web service for user register
		public function register()
		{
			$this->output->set_content_type('application/json');
			
	
			$UserId = $this->input->get("UserId", true);
			$EMail = $this->input->get("EMail", true);
			$UserName = $this->input->get("UserName", true);
			$CellPhone = $this->input->get("CellPhone", true);
			$Password = $this->input->get("Password", true);
			
			if($UserId && $EMail && $UserName && $CellPhone && $Password)
			{
				
				if($this->User->verifyUser($UserId, $UserName, $Password, $CellPhone, $EMail) == FALSE)
				{
					return $this->output->set_output(json_encode(array("error" => "Register Information Error")));
				}
				
				$user = $this->User->createUser($UserId, $UserName, $Password, $CellPhone, $EMail);
				
				if($user)
				{
					$this->__register_mail($user);
					
					return $this->output->set_output(json_encode($user));
				}
				
				return $this->output->set_output(json_encode(array("error" => "Register Fail")));
			}
			
			return $this->output->set_output(json_encode(array("error" => "Unkown Error")));
		}
		
		//web service for user to update account information
		public function update()
		{
			
			$this->load->library('session');
			
			$this->output->set_content_type('application/json');
			
			if($this->session->userdata("user") === FALSE)
			{
				return $this->output->set_output(json_encode(array("error" => "User Not Login")));
			}
	
			$EMail = $this->input->get("EMail", true);
			$UserName = $this->input->get("UserName", true);
			$CellPhone = $this->input->get("CellPhone", true);
			$Password = $this->input->get("Password", true);
			
			$user = $this->session->userdata("user");
			
			if($EMail && $UserName && $CellPhone)
			{
				
				if($this->User->verifyUserUpdate($user, $UserName, $CellPhone, $EMail) == FALSE)
				{
					return $this->output->set_output(json_encode(array("error" => "Update User Information Error")));
				}
				
				$user = $this->User->updateUser($user->UserId, $UserName, $CellPhone, $EMail);
				
				if($user)
				{					
					$this->session->set_userdata(array("user" => $user));
					return $this->output->set_output(json_encode($user));
				}
				
				return $this->output->set_output(json_encode(array("error" => "Update Fail")));
			}
			
			return $this->output->set_output(json_encode(array("error" => "Unkown Error")));
		}
		
		
		//web service for user find password
		public function find_password()
		{
			
			$this->output->set_content_type('application/json');
			
	
			$UserId = $this->input->get("UserId", true);
			$EMail = $this->input->get("EMail", true);
			$UserName = $this->input->get("UserName", true);
			$CellPhone = $this->input->get("CellPhone", true);
			
			if($UserId && $EMail && $UserName && $CellPhone)
			{
				
				$user = $this->User->getUserById($UserId);
				
				if($user)
				{
					if($user->EMail !== $EMail || $user->UserName !== $UserName || $user->CellPhone !== $CellPhone)
					{
						return $this->output->set_output(json_encode(array("error" => "User Infomation Error")));
					}
					$user = $this->User->updatePasswordDirect($user, substr($user->UserId, 12));
					$this->__find_password_mail($user, substr($user->UserId, 12));
					
					return $this->output->set_output(json_encode($user));
				}
				
				return $this->output->set_output(json_encode(array("error" => "User Not Found")));
			}
			
			return $this->output->set_output(json_encode(array("error" => "Unkown Error")));
		}
				
		//web service for user to update password
		public function update_password()
		{
			$this->load->library('session');
			
			$this->output->set_content_type('application/json');
			
			if($this->session->userdata("user") === FALSE)
			{
				return $this->output->set_output(json_encode(array("error" => "User Not Login")));
			}
			
			$user = $this->session->userdata("user");
	
			$OldPassword = $this->input->get("OldPassword", true);
			$NewPassword = $this->input->get("NewPassword", true);
			$NewPasswordAgagin = $this->input->get("NewPasswordAgain", true);
						

			if($OldPassword && $NewPassword && $NewPasswordAgagin)
			{
				
				if($NewPassword != $NewPasswordAgagin)
				{
					return $this->output->set_output(json_encode(array("error" => "New Password Not Match")));
				}
				
				$user = $this->User->updatePassword($user, $OldPassword, $NewPassword);
				
				if( $user !== FALSE )
				{
					$this->__update_password_mail($user, $NewPassword);
					return $this->output->set_output(json_encode($user));
					
				}else{
					
					return $this->output->set_output(json_encode(array("error" => "Update Password Error")));
					
				}
			}
			
			return $this->output->set_output(json_encode(array("error" => "Unkown Error")));
		}
		

		
		function __register_mail($user)
		{
			$this->load->library('email');
			$this->load->library('parser');

			$data = array(
				'UserName' => $user->UserName,
				'SiteName' => $this->config->item("site_name"),
				'SiteURL' => $this->config->item("site_url")
			);

			$content = $this->parser->parse('mail/register', $data, TRUE);

			$this->email->initialize(array(
				'mailtype' => "html"
			));
			$this->email->from($this->config->item("site_email"), $this->config->item("site_name"));
			$this->email->to($user->EMail); 
			
			$this->email->subject(''.$this->config->item("site_name"));
			$this->email->message($content);	
			
			$this->email->send();
			
		}
		
		function __find_password_mail($user, $Password)
		{
			$this->load->library('email');
			$this->load->library('parser');

			$data = array(
				'UserName' => $user->UserName,
				'SiteName' => $this->config->item("site_name"),
				'SiteURL' => $this->config->item("site_url"),
				'Password' => $Password
			);

			$content = $this->parser->parse('mail/find_password', $data, TRUE);

			$this->email->initialize(array(
				'mailtype' => "html"
			));
			$this->email->from($this->config->item("site_email"), $this->config->item("site_name"));
			$this->email->to($user->EMail); 
			
			$this->email->subject('您在'.$this->config->item("site_name")."的密码已经重设");
			$this->email->message($content);	
			
			$this->email->send();
			
			//echo $this->email->print_debugger();
		}
		
		function __update_password_mail($user, $NewPassword)
		{
			$this->load->library('email');
			$this->load->library('parser');

			$data = array(
				'UserName' => $user->UserName,
				'SiteName' => $this->config->item("site_name"),
				'SiteURL' => $this->config->item("site_url"),
				'NewPassword' => $NewPassword
			);

			$content = $this->parser->parse('mail/update_password_mail', $data, TRUE);

			$this->email->initialize(array(
				'mailtype' => "html"
			));
			$this->email->from($this->config->item("site_email"), $this->config->item("site_name"));
			$this->email->to($user->EMail); 
			
			$this->email->subject('您在'.$this->config->item("site_name")."的密码已经更新");
			$this->email->message($content);	
			
			$this->email->send();
			
			//echo $this->email->print_debugger();
		}
		
	}
?>